package com.vegetable.modules.controller.inbound;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vegetable.common.annotation.PreventDuplicateSubmit;
import com.vegetable.common.annotation.SysLog;
import com.vegetable.common.utils.PageUtils;
import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.inbound.PurchaseDelivery;
import com.vegetable.modules.entity.inbound.po.PurchaseDeliverySequencePO;
import com.vegetable.modules.service.inbound.IPurchaseDeliveryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 入库-采购收货表 前端控制器
 * </p>
 *
 * @author wangruijie
 * @since 2024-03-09
 */
@Slf4j
@RestController
@RequestMapping("/inStorage/purchaseDelivery")
@AllArgsConstructor
@Api(value = "入库采购收货", tags = "入库采购收货")
public class PurchaseDeliveryController extends AbstractController {

    private IPurchaseDeliveryService purchaseDeliveryService;
    /**
     * 所有入库采购收货列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "条件查询入库采购收货列表", notes = "条件查询入库采购收货列表")
    public R list(@RequestParam Map<String, Object> params){
        log.info("条件查询入库采购收货列表{}", JSON.toJSONString(params));
        PageUtils page = purchaseDeliveryService.queryPage(params);
        return R.data( page);
    }

    /**
     * 详情入库采购收货
     */
    @SysLog(value = "详情入库采购收货")
    @GetMapping("/detailPurchaseDelivery")
    @ApiOperation(value = "根据id查询详情入库采购收货", notes = "根据id查询详情入库采购收货")
    public R detailPurchaseDelivery(Long id){
        if (id == null || id == -1){
            return R.fail("订单主键不能为空");
        }
        log.info("根据id查询详情入库采购收货：{}", JSON.toJSONString(id));
        return purchaseDeliveryService.detailPurchaseDelivery(id);
    }
    
    /**
     * 新增入库采购收货
     */
    @SysLog(value = "新增入库采购收货")
    @PostMapping("/savePurchaseDelivery")
    @ApiOperation(value = "新增入库采购收货", notes = "新增入库采购收货")
    public R savePurchaseDelivery(@RequestBody PurchaseDelivery purchaseDelivery){
        log.info("新增入库采购收货：{}", JSON.toJSONString(purchaseDelivery));
        purchaseDelivery.setWorkerId(getUserId());
        purchaseDelivery.setWorkerName(getUser().getRealName());
        purchaseDelivery.setCreateUserId(getUserId());
        try {
            return purchaseDeliveryService.savePurchaseDelivery(purchaseDelivery);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("错误信息:{}",e.getLocalizedMessage());
            return R.fail(e.getMessage());
        }
    }

    /**
     * 修改入库采购收货
     */
    @SysLog(value = "修改入库采购收货")
    @PostMapping("/updatePurchaseDelivery")
    @ApiOperation(value = "修改入库采购收货", notes = "修改入库采购收货")
    public R updatePurchaseDelivery(@RequestBody PurchaseDelivery purchaseDelivery) throws Exception {
        log.info("修改入库采购收货：{}", JSON.toJSONString(purchaseDelivery));
        if (purchaseDelivery.getId() == null || purchaseDelivery.getId() == -1){
            return R.fail("订单主键不能为空");
        }
        purchaseDelivery.setCreateUserId(getUserId());
        return purchaseDeliveryService.savePurchaseDelivery(purchaseDelivery);
    }

    /**
     * 提交入库采购收货
     */
    @SysLog(value = "提交入库采购收货")
    @PostMapping("/submitPurchaseDelivery")
    @PreventDuplicateSubmit(expireTime = 5)
    @ApiOperation(value = "提交入库采购收货", notes = "修改入库采购收货")
    public R submitPurchaseDelivery(@RequestBody PurchaseDelivery purchaseDelivery){
        log.info("提交入库采购收货：{}", JSON.toJSONString(purchaseDelivery));
        purchaseDelivery.setCreateUserId(getUserId());
        try {
            return purchaseDeliveryService.submitPurchaseDelivery(purchaseDelivery);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("错误信息:{}",e.getLocalizedMessage());
            return R.fail(e.getMessage());
        }
    }

    /**
     * 删除入库采购收货
     */
    @SysLog(value = "删除入库采购收货")
    @GetMapping("/deletePurchaseDelivery")
    @ApiOperation(value = "根据id删除入库采购收货", notes = "根据id删除入库采购收货")
    public R deletePurchaseDelivery(Long id){
        log.info("删除入库采购收货：{}", JSON.toJSONString(id));
        try {
            return purchaseDeliveryService.deletePurchaseDelivery(id);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("错误信息:{}",e.getLocalizedMessage());
            return R.fail(e.getMessage());
        }
    }


    /**
     * 查询（未对账金额 > 0）可收款的采购收货
     * @param customerId null
     * @return R
     * @author wrj
     * @since 2024/4/15 上午9:31
     */
    @GetMapping("/canPaymentList")
    @ApiOperation(value = "查询可收款的采购收货", notes = "查询可收款的采购收货")
    public R canPaymentList(
            @RequestParam(value = "customerId")Long customerId,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime){
        log.info("条件查询采购收货列表{}", customerId);
        List<PurchaseDelivery> list= purchaseDeliveryService.canPaymentList(customerId, startTime, endTime);
        return R.data(list);
    }
    @PostMapping("/sequence")
    @ApiOperation(value = "采购收货总表查询", notes = "采购收货总表查询")
    public R sequence(@RequestBody PurchaseDeliverySequencePO po){
        log.info("入库代办交货总表查询{}", JSON.toJSONString(po));
        IPage page = purchaseDeliveryService.sequence(po);
        return R.data( page);
    }
}
